
let token = sessionStorage.getItem("token");
let post_id = sessionStorage.getItem("post_id");
//确定模态框
$("#my-alert").on('close.modal.amui', function () {
    window.location.reload();
});
//关闭modal1
$("#modal1").on('close.modal.amui', function () {
    $("#fileList").html("");
});
//模态框
$("#my-popup").on('close.modal.amui', function () {
    $("#title").val("");
    $("#out_link").val("");
    editor.html("");
    $("#time3").attr("value", "");
});
//封装提交后的提示
function prompt(data, title, modal) {
    if (data.success === true) {
        $(".users").html("" + title + "");
        $(".alert").html(data.message);
        $('#my-alert').modal({ target: '#my-alert' });
        $("#" + modal + "").modal('close');

    } else {
        $(".users").html("" + title + "");
        $(".alert").html(data.message);
        $('#my-alert').modal({ target: '#my-alert' });
        $("#" + modal + "").modal('close');
    }
}
// kindeditor
KindEditor.ready(function (K) {
    editor = K.create('#editor_id', {
        uploadJson: testurl + 'kindeditor/'

    });
});
$(function () {




    //分页
    let pageNum = 1;
    $(".first").click(function () {
        pageNum = 1;
        loadData(pageNum);
        $(this).parents("li").addClass("am-active");
        $(this).parents("li").siblings().removeClass("am-active");
    });
    $(".last").click(function () {
        pageNum = pages;
        loadData(pageNum);
        $(this).parents("li").addClass("am-active");
        $(this).parents("li").siblings().removeClass("am-active");
    });
    $(".up").click(function () {
        if (pageNum === 1) {
            return false;
        } else {
            pageNum--;
            loadData(pageNum);
        }
        $(this).parents("li").addClass("am-active");
        $(this).parents("li").siblings().removeClass("am-active");
    });
    $(".down").click(function () {
        if (pageNum === pages) {
            return false;
        } else {
            pageNum++;
            loadData(pageNum);
        }
        $(this).parents("li").addClass("am-active");
        $(this).parents("li").siblings().removeClass("am-active");
    });


    loadData(1);
    let user_ids = [];
    let checkedLength = "";
    $("#dbdelete").click(function () {
        $.each($('#show input:checkbox:checked'), function () {
            let userId = $(this).val();

            user_ids.push(userId);
        });
        checkedLength = $('#show input:checkbox:checked').length;
        if (checkedLength <= 0) {
            alert("请选中至少一条资料！");
            return false;
        } else {
            if (confirm("确定删除所选资料？")) {
                $.ajax({
                    url: testurl + "duty/deletes",
                    type: "post",
                    headers: { "Authorization": "Bearer " + token },
                    contentType: "application/json",
                    data: JSON.stringify({
                        "ids": user_ids //id数组
                    }),
                    success: function (data) {
                        prompt(data, "删除资料", "")
                    }
                })
            }

        }
    })
});
//页数
let pages = 1;
function loadData(pageNum) {
    $.ajax({

        url: testurl + "duty/video",
        type: "post",
        headers: { "Authorization": "Bearer " + token },
        contentType: "application/json",
        data: JSON.stringify({
            "pageNum": pageNum
        }),
        success: function (data) {
            let dbmessage = data.obj;
            ssd = Math.ceil(data.pageTotal / data.pageSize);
            pages = ssd;
            str = "";
            $.each(dbmessage, function (index, values) {
                if (values.deputy_group_name === undefined) {
                    values.deputy_group_name = "";
                }
                if (values.deputy_team_name === undefined) {
                    values.deputy_team_name = "";
                }
                if (values.post_name === undefined) {
                    values.post_name = "";
                }
                if (values.deputy_no === undefined) {
                    values.deputy_no = "";
                }
                str += " <tr class=\"gradeX\">\n" +
                    "                            <td><input name='selectAll'  class='checked' value=" + values.id + " type='checkbox'></td>\n" +
                    "                                        <td><a style=\"cursor:pointer\" data-am-modal=\"{target:'#modal1'}\" onclick=\"getMsg(" + values.id + ")\">" + values.title + "</a></td>\n" +
                    // "                                        <td><a download href=\"" + testurl + "" + values.file_path + "\">" + values.file_name + "</a></td>\n" +
                    "                                        <td>" + values.create_user_name + "</td>\n" +
                    "                                        <td>" + values.pub_times + "</td>\n"+
                    "      <td>\n" +
                    "    <button class=\"btnImg60 quan_xian\" rules=\"video_xiu\" data-am-modal=\"{target: '#my-popup'}\"  onclick='editor1(" + values.id + ");'>修改</button>\n" +

                    "    <button class=\"btnImg60\"  data-am-modal=\"{target: '#modal1'}\"  onclick='getMsg(" + values.id + ");'>查看</button>\n" +
                        "     </td>\n"+
                    "                                    </tr>";
            });
            $("#show").html(str);
            rules();
        }
    });
}


//选中所有的checkbox框
$('#checkAll[name="selectAll"]').click(function () {
    if ($(this).is(':checked')) {
        $('#show input[name="selectAll"]').each(function () {

            $(this).prop("checked", true);
        });
    } else {
        $('input[name="selectAll"]').each(function () {
            $(this).removeAttr("checked", false);
        });
    }
});




// 添加新闻
function submitMsg() {
    // 视频资料文件上传
    // let file_path = "";
    // let file_name = "";
    let file_list = [];
    let formData = new FormData();
    let pic1 = document.getElementById("fileUpload1").files;

    for (let j = 0; j < pic1.length; j++) {
        formData.append("" + j + "", pic1[j]);
    }
    $.ajax({
        url: testurl + "duty/file/1",
        type: 'post',
        headers: { "Authorization": "Bearer " + token },
        data: formData,
        async: false,
        cache: false,
        contentType: false,
        processData: false,
        success: function (data) {
            // $.each(data.obj,function(index,value){
            //     if (pic1[0] != undefined && pic1[0].name != '') {
            //         $("#upfile1").attr("name", data.obj.name);
            //         $("#upfile1").attr("path", data.obj.path);
            //     }
            //     file_path = data.obj.path;
            //     file_name = data.obj.name;
            // })
            console.log(data);
            file_list = data.obj;

        },
        error: function (data) {
            console.log(data);
        }
    });
    let video_pic_path = "";
    let video_pic_name = "";
    let formData2 = new FormData();
    let pic2 = document.getElementById("fileUpload2").files;

    for (let j = 0; j < pic2.length; j++) {
        formData2.append("" + j + "", pic2[j]);
    }
    $.ajax({
        url: testurl + "duty/file/video_pic",
        type: 'post',
        headers: { "Authorization": "Bearer " + token },
        data: formData2,
        async: false,
        cache: false,
        contentType: false,
        processData: false,
        success: function (data) {
            // if (pic2[0] != undefined && pic2[0].name != '') {
            //     if (pic1[0] != undefined) {
            //         $("#upfile2").attr("name", data.obj.name);
            //         $("#upfile2").attr("path", data.obj.path);
            //     } else {
            //         $("#upfile2").attr("name", data.obj.name);
            //         $("#upfile2").attr("path", data.obj.path);
            //     }
            // }
            video_pic_path = data.obj.path;
            video_pic_name = data.obj.name;
        },
        error: function (data) {
            console.log(data);
        }
    });
    let title = $("#title").val();
    let out_link = $("#out_link").val();
    let pub_times = $("#time3").val();
    let content = editor.html();

    if (title === "" || pub_times === "") {
        alert("请填写视频标题和发布时间！");
        return false;
    } else {


        $.ajax({
            async: false,
            url: testurl + "duty/",
            type: "post",
            headers: { "Authorization": "Bearer " + token },
            contentType: "application/json",
            data: JSON.stringify({
                "type": "1",
                "title": title,//标题
                file_list: file_list,
                "out_link": out_link,
                "content": content,
                "pub_times": pub_times,
                "video_pic_path": video_pic_path,//缩略图路径 视频用
                "video_pic_name": video_pic_name//缩略图名称 视频用
            }),
            success: function (data) {
                prompt(data, "添加视频资料", "my-popup");
            }
        })
    }


}
//修改
function editor1(e) {

    $("#getId").val(e);
    $("#add").hide();
    $("#rechange").show();
    $("#file1").show();
    $("#file2").show();
    let str = "";
    let str2 = "";
    // $("#file2").show();
    $.ajax({
        url: testurl + "duty/detail",
        type: "post",
        headers: { "Authorization": "Bearer " + token },
        contentType: "application/json",
        data: JSON.stringify({
            "id": e
        }),
        success: function (data) {
            console.log(data)
            $.each(data.obj.file_list, function (index, value) {
                str += "<a target=\"_blank\" href=\"" + testurl + "" + value.path + "\">" + value.name + "</a> <input type=\"checkbox\" path=\"" + value.path + "\" name=\"" + value.name + "\" name2=\"fileList\"> "
            })
            $("#file1").html(str);
            $("#file2").html("<a target=\"_blank\" href=\"" + testurl + "" + data.obj.video_pic_path + "\">" + data.obj.video_pic_name + "</a> <input type=\"checkbox\" path=\"" + data.obj.video_pic_path + "\" name=\"" + data.obj.video_pic_name + "\" name2=\"pic\"> ")
            let aaa = data.obj;
            $("#title").val(aaa.title);
            $("#out_link").val(aaa.out_link);
            editor.html(aaa.content);
            $("#time3").attr('value', aaa.pub_times);
            console.log(aaa.pub_times)
        }
    })
}
//清楚文件
function clearFile() {
    $("#file1").hide();
    $("#file2").hide();
    $("#add").show();
    $("#rechange").hide();
}
//修改
function rechangeMsg() {
    let id = $("#getId").val();
    let title = $("#title").val();
    let out_link = $("#out_link").val();
    let pub_time = $("#time3").val();
    let content = editor.html();
    // 图片上传

    let file_list = [];
 

    let formData = new FormData();
    let pic1 = document.getElementById("fileUpload1").files;

    for (let j = 0; j < pic1.length; j++) {
        formData.append("" + j + "", pic1[j]);
    }
    $.ajax({
        url: testurl + "duty/file/1",
        type: 'post',
        headers: { "Authorization": "Bearer " + token },
        data: formData,
        async: false,
        cache: false,
        contentType: false,
        processData: false,
        success: function (data) {
            if (data.obj == null) {
                file_list = [];
            } else {
                file_list = data.obj;
            }
        },
        error: function (data) {
            console.log(data);
        }
    });
 
    let video_pic_path = "";
    let video_pic_name = "";
    let formData2 = new FormData();
    let pic2 = document.getElementById("fileUpload2").files;

    for (let j = 0; j < pic2.length; j++) {
        formData2.append("" + j + "", pic2[j]);
    }
    $.ajax({
        url: testurl + "duty/file/video_pic",
        type: 'post',
        headers: { "Authorization": "Bearer " + token },
        data: formData2,
        async: false,
        cache: false,
        contentType: false,
        processData: false,
        success: function (data) {
            // if (pic2[0] != undefined && pic2[0].name != '') {
            //     if (pic1[0] != undefined) {
            //         $("#upfile2").attr("name", data.obj.name);
            //         $("#upfile2").attr("path", data.obj.path);
            //     } else {
            //         $("#upfile2").attr("name", data.obj.name);
            //         $("#upfile2").attr("path", data.obj.path);
            //     }
            // }
            video_pic_path = data.obj.path;
            video_pic_name = data.obj.name;
        },
        error: function (data) {
            console.log(data);
        }
    });


    $.ajax({
        url: testurl + "duty/update",
        type: "post",
        headers: { "Authorization": "Bearer " + token },
        contentType: "application/json",
        data: JSON.stringify({
            "id": id,
            "type": "1",
            "title": title,//标题
            "file_list": file_list,
            "out_link": out_link,
            "pub_times": pub_time,
            "content": content,
            "video_pic_path": video_pic_path,
            "video_pic_name": video_pic_name
        }),
        success: function (data) {
            prompt(data, "修改视频资料", "my-popup");
        }
    })
}
//标题点开
function getMsg(e) {
    let str = "";
    $.ajax({
        url: testurl + "duty/detail",
        type: "post",
        headers: { "Authorization": "Bearer " + token },
        contentType: "application/json",
        data: JSON.stringify({
            "id": e
        }),
        success: function (data) {
            console.log(data);
            $("#title1").html(data.obj.title);
            $("#create_times").html(data.obj.pub_times);
            $("#create_user_name").html(data.obj.create_user_name);
            $("#title2").html(data.obj.title);
            $("#content1").html(data.obj.content);
            $.each(data.obj.file_list, function (index, value) {
                str += "<li>视频：<a download href=\"" + testurl + "" + value.path + "\">" + value.name + "</a></li>"
            })
            $("#fileList").append(str);
            $("#fileList").append("<li>缩略图：<a download href=\"" + testurl + "" + data.obj.video_pic_path + "\">" + data.obj.video_pic_name + "</a></li>");
        }
    })
}